/**
 * 该模块用于连接数据库, 暴露出去一个Promise实例
 * 如果连接成功了, Promise实例内部状态切换为成功
 * 如果连接失败了, Promise实例内部状态切换为失败
 */
let mongoose = require("mongoose")
mongoose.set("useCreateIndex", true)

let DB_NAME = "demo"
let DB_URL = "localhost:27017"

module.exports = new Promise((resolve, reject) => {
    mongoose.connect(`mongodb://${DB_URL}/${DB_NAME}`, {useNewUrlParser: true})
    mongoose.connection.on("open", (err) => {
        if(!err){
            console.log(`位于 ${DB_URL} 上的 ${DB_NAME} 数据库连接成功!`)
            resolve()
        }else{
            console.log(err)
            reject(err)
        }
    })
})